package main

import (
	"fmt"
	"strconv"
)

func main() {
	// strconv 基本数据类型转为字符串
	// 最常见的数值转换是 Atoi(string to int)和 Itoa(int to string)。

	atoi, _ := strconv.Atoi("-42")

	itoa := strconv.Itoa(atoi)

	println(atoi, itoa)

	//解析函数返回最宽的类型（float64，int64和uint64），但如果 size 参数指定较窄的宽度，则结果可以转换为较窄的类型而不会丢失数据：
	s := "333333332" // biggest int32
	i64, _ := strconv.ParseInt(s, 10, 64)

	i := int32(i64)

	fmt.Println(i64, i)

	fmt.Println("========================================")
	b := []byte("bool:")
	b = strconv.AppendBool(b, true)
	fmt.Println(string(b))

	fmt.Println("========================================")

	b10 := []byte("int (base 10):")
	b10 = strconv.AppendInt(b10, -42, 10)
	fmt.Println(string(b10))
	// 16进制转换
	b16 := []byte("int (base 16):")
	b16 = strconv.AppendInt(b16, -42, 16)
	fmt.Println(string(b16))

	fmt.Println("========================================")
	quote := []byte("quote:")
	quote = strconv.AppendQuote(quote, `"Fran & Freddie's Diner"`)
	fmt.Println(string(quote))

	fmt.Println("========================================")
	v := 3.1415926535
	s32 := strconv.FormatFloat(v, 'E', -1, 32)
	fmt.Printf("%T, %v\n", s32, s32)

	s64 := strconv.FormatFloat(v, 'E', -1, 64)
	fmt.Printf("%T, %v\n", s64, s64)

	fmt.Println("========================================")

	int641 := int64(-42)

	s10 := strconv.FormatInt(int641, 10)
	fmt.Printf("%T, %v\n", s10, s10)

	s16 := strconv.FormatInt(int641, 16)
	fmt.Printf("%T, %v\n", s16, s16)

	fmt.Println("========================================")

	str := "Not a number"
	if _, err := strconv.ParseFloat(str, 64); err != nil {
		e := err.(*strconv.NumError)
		fmt.Println("Func:", e.Func)
		fmt.Println("Num:", e.Num)
		fmt.Println("Err:", e.Err)
		fmt.Println(err)
	}

}
